import axios from 'axios';

class HttpClient {
  constructor(baseURL = '', timeout = 10000) {
    this.baseURL = baseURL;
    this.timeout = timeout;
    this.instance = axios.create({
      baseURL: this.baseURL,
      timeout: this.timeout,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }
    });

    // 设置响应拦截器
    this.instance.interceptors.response.use(
      response => response.data,
      error => Promise.reject(error)
    );
  }

  // GET 请求
  get(url, params = {}, config = {}) {
    return this.instance.get(url, { params, ...config });
  }

  // POST 请求
  post(url, data = {}, config = {}) {
    return this.instance.post(url, data, config);
  }

  // PUT 请求
  put(url, data = {}, config = {}) {
    return this.instance.put(url, data, config);
  }

  // DELETE 请求
  delete(url, config = {}) {
    return this.instance.delete(url, config);
  }

  // PATCH 请求
  patch(url, data = {}, config = {}) {
    return this.instance.patch(url, data, config);
  }

  // 通用请求方法
  request(config) {
    return this.instance(config);
  }
}

export default HttpClient;
// 创建 HttpClient 实例
export const baseHttpClient = new HttpClient('/api');